<?php
/**
 * Utils
 *-------------------------
 *
 * Deal with menu display, selected status. Top menu - sub menu 
 *
 * PHP versions 5
 *
 * LICENSE: This source file is from Smarter Ver4.0, which is a comprehensive shopping engine
 * that helps consumers to make smarter buying decisions online. We empower consumers to compare
 * the attributes of over one million products in the computer and consumer electronics categories
 * and to read user product reviews in order to make informed purchase decisions. Consumers can then
 * research the latest promotional and pricing information on products listed at a wide selection of
 * online merchants, and read user reviews on those merchants.
 * The copyrights is reserved by http://www.mezimedia.com.
 * Copyright (c) 2005, Mezimedia. All rights reserved.
 *
 * @author     james.huang <james_huang@mezimedia.com>
 * @copyright  (C) 2004-2008 Mezimedia.com
 * @license    http://www.mezimedia.com  PHP License 5.2
 * @version    CVS: $Id: UserController.php
 * @link       http://pms.mezimedia.com/
 * @deprecated File deprecated in Release 1.0
 */
 
class Utils
{
    ///////////////////////////////////////////////////////////////////////////////////////////////
    // string & array method
	
	public static function dumper ($array) {
		echo "<pre>\n";
		print_r($array);
		echo "</pre>\n";
	}
	
	public static function truncate ($string, $length = 80, $etc = '...', $break_words = false) {
		if ($length == 0) return '';
		if (strlen($string) > $length) {
			if (!$break_words)
			$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
			return substr($string, 0, $length).$etc;
		} else {
			return $string;
		}
	}
	
	public static function array_remove ($arr, $rem) {
		foreach ($arr as $k => $v) {
			if (is_array($rem)) {
				if (in_array($v, $rem)) {
					$arr[$k] = null;
				}
			} else {
				if ($v == $rem) {
					$arr[$k] = null;
					break;
				}
			}
		}
		return array_filter($arr);
	}
	
    ///////////////////////////////////////////////////////////////////////////////////////////////
    // db operation method
    
    public static function db_mk_row_data ($cols = array(), $params) {
    	$data = array();
    	// fill row data with params
    	foreach ($cols as $column) {
    		foreach ($params as $k => $v) {
    			if (!strcmp($column, $k) && strlen(trim($v)) > 0) {
    				$data[$column] = trim($v);
    			}
    		}
    	}
    	// remove id if exists
    	unset($data['id']);
    	
    	return $data;
    }

	
	public function getDataRange($inputDate){

		$inputDateArray=getdate($this->GetMkTime($inputDate));
		if($inputDateArray['wday']==1){
			
			$startData=$inputDate;
			$endData=date('Y-m-d',$this->GetMkTime($inputDate)+(4 * 24 * 60 * 60));
		}
		elseif($inputDateArray['wday']==5){
			
			$startData=date('Y-m-d',$this->GetMkTime($inputDate)-(4 * 24 * 60 * 60));
			$endData=$inputDate;
		}
		else{
		
			$startData = date('Y-m-d',$this->GetMkTime($inputDate)-(($inputDateArray['wday']-1) * 24 * 60 * 60));
			$endData = date('Y-m-d',$this->GetMkTime($inputDate)+((5-$inputDateArray['wday']) * 24 * 60 * 60));
		}
		$returnDataRangeArr[0]=$startData;
		$returnDataRangeArr[1]=$endData;

		return $returnDataRangeArr;
	}

	public function GetMkTime($dtime)
	{
		$y=1970;
		$m=1;
		$d=1;
		$h=0;
		$mm=0;
		$s=0;
		$dtime = str_replace("-0","-",$dtime);
		$dtime = str_replace(":0",":",$dtime);
		$ds = explode(" ",$dtime);
		list($y,$m,$d) = explode("-",$ds[0]);
		if(count($ds)==2)
		{
			list($h,$mm,$s) = explode(":",$ds[1]);
		}
		$mt = @mktime($h,$mm,$s,$m,$d,$y);
		if($mt>0) return $mt;
		else return 0;
	}

	public function SubDay($ntime,$ctime)
	{
		$dayst = $this->GetMkTime("2006-01-02") - $this->GetMkTime("2006-01-01");
		return ceil(($this->GetMkTime($ntime)-$this->GetMkTime($ctime))/$dayst);
	}
}
?>